package cn.edu.zucc.online_market.ui;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.edu.zucc.online_market.model.BeanUser;
import cn.edu.zucc.online_market.online_marketUtil;
import cn.edu.zucc.online_market.util.*;;
public class FrmLogin extends JDialog implements ActionListener{
//		������
	private JLabel label_telephone = new JLabel("手机号");
	private JTextField text_telephone = new JTextField(20);
	private JLabel label_password = new JLabel("密码");
	private JPasswordField pwd = new JPasswordField(20);
	private JButton button_register = new JButton("注册");
	private JButton button_login = new JButton("登录");
	private JButton button_exit = new JButton("退出");
	private JRadioButton user_type = new JRadioButton("管理员");
	
	//	����
	private JPanel panel1 = new JPanel();
	private JPanel panel2 = new JPanel();
	private JPanel panel3 = new JPanel();
	private JPanel workPane = new JPanel();
	//	������
	public FrmLogin(JFrame f,String s, boolean b) {
		super(f,s,b);
		
//		user_type.setSelected(true);
		panel1.add(label_telephone);
		panel1.add(text_telephone);
		
		
		panel2.add(label_password);
		panel2.add(pwd);
		
		panel3.add(user_type);
		panel3.add(button_login);
		panel3.add(button_register);
		panel3.add(button_exit);
		
		workPane.add(panel1);
		workPane.add(panel2);
		workPane.add(panel3);
		
		this.getContentPane().add(workPane);
	
//		��Ӽ���
		button_register.addActionListener(this);
		button_login.addActionListener(this);
		button_exit.addActionListener(this);
		this.setSize(400,300);
		this.setResizable(false);
	}
	
	//��������
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==this.button_register) {
			FrmRegister frg = new FrmRegister(this ,"注册" ,true);
			frg.setVisible(true);
		}else if(e.getSource()==this.button_login) {
			String user_telephone = this.text_telephone.getText();
			String user_pwd = new String(this.pwd.getPassword());
			if(test_telephone_pwd(user_telephone,user_pwd)){
				if(this.user_type.isSelected()) {
					//管理员登录
					
				}else {
					//用户登录
					try {
						BeanUser.currentLoginUser = online_marketUtil.userManager.login(user_telephone, user_pwd);
						BeanUser.currentCart = online_marketUtil.userManager.loadCart(); 
					} catch (BaseException e1) {
						// TODO Auto-generated catch block
						JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR",JOptionPane.ERROR_MESSAGE);
						return;
					}
					JOptionPane.showMessageDialog(null, "login successed", "FrmMain not build",JOptionPane.ERROR_MESSAGE);
					this.setVisible(false);
				}
			}	
		}else if(e.getSource()==this.button_exit) {
			System.exit(0);
		}
	}
	public boolean test_telephone_pwd(String telephone,String pwd) {
		if("".equals(telephone) || telephone==null) JOptionPane.showMessageDialog(null, "telephone is null", "ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(pwd) || pwd==null) JOptionPane.showMessageDialog(null, "pwd is null", "ERROR",JOptionPane.ERROR_MESSAGE);
		else return true;
		return false;
	}
	public static void main(String [] args) {
		FrmLogin a=new FrmLogin(new JFrame(),"登录",true);
		a.setVisible(true);
	}
}
